package LinkedList.交换节点_两两;

import LinkedList.ListNode;

public class Test {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode cur = head;
        ListNode dummy = new ListNode(-1);
        ListNode prev = dummy;
        dummy.next = head;
        while(cur != null && cur.next != null) {
            ListNode next = cur.next;
            cur.next = next.next;
            next.next = cur;
            if(prev.next != next) {
                prev.next = next;
                prev = cur;
            }
            cur = cur.next;
        }
        return dummy.next;
    }
}
